import axios from "axios"
import {message} from "antd";

axios.defaults.baseURL = "http://localhost:3003"

// withCredentials是XMLHttpRequest的一个属性，
// 表示跨域请求是否提供凭据信息(cookie、HTTP认证及客户端SSL证明等)
axios.defaults.withCredentials = true;

/*请求拦截器*/
axios.interceptors.request.use((config) => {
    // 登录
    if (config.url.includes('/login') || config.url.includes('/register')) {
        return config
    }
    // 图片上传
    if (config.url.includes('/upload')) {
        config.headers['Content-Type'] = "multipart/form-data";
    }
    let {token} = sessionStorage
    if (token) {
        config.headers.Authorization = `Bearer ${token}`
    }
    return config
})


/*相应拦截器*/
axios.interceptors.response.use((res) => {
    return res.data
})